<?php
namespace App\Http\Controllers;

use App\Traits\WorkerRedisConnect;
use Illuminate\Support\Facades\Redis;

class HandleRedis extends Redis
{
    use WorkerRedisConnect;

    /**
     * 给用户加锁
     * @param $uid
     * @return bool
     */
    public static function addUserLock($uid)
    {
        return self::connectWorker()->setnx('sql_lock'.$uid,'1');
    }

    /**
     * 给用户删锁
     * @param $uid
     * @return mixed
     */
    public static function delUserLock($uid)
    {
        return self::connectWorker()->del('sql_lock'.$uid);
    }

    /**
     * 给桌子加锁
     * @param $showhao
     * @return bool
     */
    public static function addTableLock($showhao)
    {
        return self::connectWorker()->setnx('sql_lock'.$showhao,'1');
    }

    /**
     * 给桌子删锁
     * @param $showhao
     * @return int
     */
    public static function delTableLock($showhao)
    {
        return self::connectWorker()->del('sql_lock'.$showhao);
    }

    /**
     * 存储所有机器人信息
     * @param array $value
     */
    public static function setAllRobot($value=[])
    {
        $redis = self::connectWorker();
        foreach ($value as $robot) {
            $redis->hSet("jiqirenMessage",$robot['id'],toJson($robot));
        }
    }

    /**
     * 获取所有机器人数据
     * @param string $robotId
     * @return array
     */
    public static function getAllRobot($robotId='')
    {
        $redis = self::connectWorker();
        if($robotId) {
            $allRobot[$robotId] = $redis->hGet("jiqirenMessage",$robotId);
        } else {
            $allRobot = $redis->hGetAll("jiqirenMessage");
        }
        foreach ($allRobot as $k=>$v) $allRobot[$k] = toArray($v);
        return $allRobot;
    }

    /**
     * 存储所有限红
     * @param array $value
     */
    public static function setAllXianhong($value=[])
    {
        $redis = self::connectWorker();
        foreach ($value as $xianhong) {
            $redis->hSet('xianhong',$xianhong['id'],toJson($xianhong));
        }
    }

    /**
     * 获取所有限红
     * @param string $tableId
     * @return array
     */
    public static function getAllXianhong($tableId='')
    {
        $redis = self::connectWorker();
        if($tableId) {
            $allXianhong[$tableId] = $redis->hGet("xianhong",$tableId);
        } else {
            $allXianhong = $redis->hGetAll("xianhong");
        }
        foreach ($allXianhong as $k=>$v) $allXianhong[$k] = toArray($v);
        return $allXianhong;
    }

    /**
     * 根据showhao获取限红
     * @param string $showhao
     * @return array
     */
    public static function getXianhongByShowhao($showhao = '')
    {
        $allXianhong = self::getAllXianhong();
        if($showhao) {
            foreach ($allXianhong as $k=>$v) {
                if($v['showhao'] == $showhao) return $v;
            }
            return [];
        }
        return $allXianhong;
    }

    /**
     * 获取所有机器人投注信息
     * @param array $data
     * @return array
     */
    public static function getAllRobotBetContent($data = [])
    {
        $allRobotBet = (self::connectWorker())->hGetAll('jiqirenContent');
        foreach ($allRobotBet as $v) $data[] = toArray($v);
        return $data;
    }

    /**
     * 存储所有机器人投注信息
     * @param array $data
     */
    public static function setAllRobotBetContent($data = [])
    {
        foreach ($data as $k=>$v) $data[$k] = toJson($v);
        self::connectWorker()->hMSet('jiqirenContent',$data);
    }

    /**
     * 保存所有开奖数据
     * @param array $data
     */
    public static function setAllResultData($data = [])
    {
        foreach ($data as $k=>$v) $data[$k] = toJson($v);
        self::connectWorker()->hMSet('result',$data);
    }

    /**
     * 保存单个开奖数据
     * @param $key
     * @param $value
     */
    public static function setResultData($key,$value)
    {
        self::connectWorker()->hSet('result',$key,toJson($value));
    }

    /**
     * 获取所有开奖数据
     * @param array $data
     * @return array|string
     */
    public static function getAllResultData($data = [])
    {
        $allResultData = (self::connectWorker())->hGetAll('result');
        foreach ($allResultData as $k=>$v) $data[$k] = toArray($v);
        return $data;
    }

    /**
     * 设置状态并返回bool值
     * @param $key
     * @param $value
     * @return bool
     */
    public static function setTableStatus($key,$value)
    {
        $redis = self::connectWorker();
        if($value != $redis->hGet('status',$key)) {
            $redis->hSet('status',$key,$value);
            return $value;
        }
        return false;
    }


    /**
     * 返回该台子的是否停止下注
     * @param $tableId
     * @return array
     */
    public static function getTableStatus($tableId = '')
    {
        $redis = self::connectWorker();
        if($tableId) {
            $tableData = $redis->hGet('status',$tableId);
        } else {
            $tableData = $redis->hGetAll('status');
        }
        return $tableData;
    }

    /**
     * 设置台子时间还有多少并返回剩余时间
     * @param $tableId
     * @param $time
     * @param $status
     */
    public static function setTableOpenTime($tableId,$time,$status)
    {
        $redis = self::connectWorker();
        if($status=='start') $redis->setex("table_time_{$tableId}",$time,$tableId);
    }

    /**
     * 返回台子的开奖时间
     * @param string $tableId
     * @return array
     */
    public static function getTableOpenTime($tableId = '')
    {
        $data = [];
        $key = "table_time_*{$tableId}";
        $redis = self::connectWorker();
        $allTableTime = $redis->keys($key);
        foreach ($allTableTime as $table) {
            $data[$redis->get($table)] = $redis->ttl($table);
        }
        return $data;
    }

    /**
     * 返回所有台子的开奖时间
     * @param string $tableId
     * @return array
     */
    public static function getAllTableOpenTime($tableId = '')
    {
        $redis = self::connectWorker();
        if($tableId) {
            $openTimeData[$tableId] = $redis->hGet('openTime',$tableId);
        } else {
            $openTimeData = $redis->hGetAll('openTime');
        }
        return $openTimeData;
    }


    /**
     * 初始化开奖时间
     * @param $key
     * @param $value
     * @return string
     */
    public static function initTableOpenTime($key,$value)
    {
        $redis = self::connectWorker();
        return $redis->hSet('openTime',$key,$value);
    }

    /**
     * 添加聊天信息(只存储50条信息)
     * @param $showhao
     * @param null $content
     * @param int $length (长度)
     */
    public static function setChat($showhao,$content = null,$length = 50)
    {
        $key = "chat_{$showhao}";
        $redis = self::connectWorker();
        if($content) {
            $redis->lpush($key,toJson($content));
            if($redis->lLen($key)>$length) $redis->rPop($key);
        }
    }

    /**
     * 获取所有聊天信息(只存储50条)
     * @param $showhao
     * @return array
     */
    public static function getAllChat($showhao = '')
    {
        $allChat = (self::connectWorker())->lRange("chat_{$showhao}",0,-1);
        foreach ($allChat as $k=>$v) $allChat[$k] = toArray($v);
        return $allChat;
    }

    /**
     * 追加用户投注信息
     * @param $showhao
     * @param $userBetData
     * @return bool|int
     */
    public static function setUserBet($showhao,$userBetData=[])
    {
        if($userBetData) {
            return (self::connectWorker())->lPush('UserMessage_'.$showhao,toJson($userBetData));
        }
    }

    /**
     * 获取用户投注信息并弹出
     * @param $showhao
     * @return array
     */
    public static function getUserBet($showhao)
    {
        $userData = [];
        while($userData[] = toArray((self::connectWorker())->rPop('UserMessage_'.$showhao)));
        return array_filter($userData);

    }

    /**
     * 获取所有用户投注信息
     * @param $showhao
     * @return array
     */
    public static function getAllUserBet($showhao = '')
    {
        $allChat = (self::connectWorker())->lRange("UserMessage_{$showhao}",0,-1);
        foreach ($allChat as $k=>$v) $allChat[$k] = toArray($v);
        return $allChat;
    }

    /**
     * 添加聊天信息(只存储50条信息)
     * @param string $content (只接收字符串)
     * @param int $length (长度)
     */
    public static function setOpenResult($content = null,$length = 50)
    {
        $key = 'openResult';
        $redis = self::connectWorker();
        if($content) {
            $redis->lpush($key,$content);
            if($redis->lLen($key)>$length) $redis->rPop($key);
        }
    }
}
